
##### Programming language
# R version 4.3.0


##### Required library installation

library(MASS)
library(invgamma)


##### Specification of dependencies and an example code

### Step 1: Source "Training code.R"

source('Training code for EIV-SVT-Frechet.R')


### Step 2: Run "[3] Evaluation code.R" with user-specified parameters such as 
# MC: the size of Monte Carlo experiments (default = 100)
# n: the size of training samples in each MC experiment (default = 100)
# p: the number of covariates (default = 50)
# N: the size of test samples in each each MC experiment (default = 1000)

source('Evaluation code for EIV-SVT-Frechet.R')


### The evaluation code consists of two parts
# A. Computing MSPE and choosing the optimal threshold parameter
# B. Computing MSE, bias, and variance


### A pre-trained result "Pre-trained models for EIV-SVT-Frechet.RData" is available
# with (MC, n, p, N) = (100, 100, 50, 1000).

load('Pre-trained models.RData')		

## Figure 1 (left)
mspe_col <- c('red', rep('white', length(lambda_seq)-1), 'black')
mspe_col[ind_lambda] <- 'blue'

par(mfrow = c(1,1))
par(mar=c(4.5,4.5,1,4.5)+0.1)
boxplot(cbind(MSPE_svt_MC, MSPE_ref_MC),
        cex.lab = 1.25, cex.axis = 1.15,
        cex = 0.25, outline = FALSE,
        ylab = 'MSPE',
        ylim = c(0.35, 0.95),
        col = mspe_col,
        xaxt = 'n')

tshd_label <- seq(1, length(lambda_seq), length.out = 7)

axis(1, tshd_label,
     cex.lab = 1.25, cex.axis = 1.15,
     labels = round(c(0,(lambda_seq)[tshd_label][-1]), 2),
     line = 0)
mtext('Threshold', 1, line = 3,
      cex = 1.25)
grid()
legend('topleft', c('REF', 'EIV', 'SVT'), cex = 1.25,
       fill = c('black', 'red', 'blue'), 
       bty = 'n')


## Table 1: MSPE
round(mean(MSPE_ref_MC), 3); round(mean(MSPE_svt_MC[,1]), 3); round(mean(MSPE_svt_MC[,ind_lambda]), 3)


## Figure 1 (right)
par(mfrow = c(1,3))

# bias
par(mar=c(4.5,4.5,1,1)+0.1)
boxplot(bias_sq_est_all_MC, outline = FALSE,
        ylab = 'Squared Bias',
        names = c('REF', 'EIV', 'SVT'),
        col = c('black', 'red', 'blue'))
grid()

# variance
par(mar=c(4.5,4.5,1,1)+0.1)
boxplot(variance_est_all_MC, outline = FALSE,
        ylab = 'Variance',
        names = c('REF', 'EIV', 'SVT'),
        col = c('black', 'red', 'blue'))
grid()

# MSE
par(mar=c(4.5,4.5,1,1)+0.1)
boxplot(MSE_all_MC, outline = FALSE,
        ylab = 'MSE',
        names = c('REF', 'EIV', 'SVT'),
        col = c('black', 'red', 'blue'))
grid()


## Table 1: Bias, variance, MSE
# bias
as.numeric(round(sqrt(apply(bias_sq_est_all_MC, 2, mean)), 3))

# variance
as.numeric(round(sqrt(apply(variance_est_all_MC, 2, mean)), 3))

# MSE
as.numeric(round(apply(MSE_all_MC, 2, mean), 3))
